


//todo: tabs, dropdown menu, other window bits, tree, animation between max/min etc, be able to "throw" windows and they carry on moving..
//events?
;(function(){
booky.fn.window = function(settings) {

    var win = document.createElement("DIV"); 
    win.className = settings.cssClass + " bookyWindow";
    var barFrag = booky.fn.getFrag("<div class='bookyTitleBar'><span class='bookyTitle'>"+settings.title+"</span><span class='bookyClose'></span><span class='bookyMax'></span><span class='bookyMin'></span></div><span class='bookyResize'></span>");

    win.appendChild(barFrag);
    
    this.each(function() {
        win.appendChild(this);
    });
    
    
    //add window to body
    document.body.appendChild(win);
    var bWin = booky(win);         //THIS SCOPE IS WRONG........
   
 
    win.onmousedown = booky.bringToTop;
    win.onmousedown({currentTarget:win});
    

    //setup drag drop etc    
    bWin.styles( {
        width:  typeof settings.width == "undefined"    ? "auto" : settings.width,
        height: typeof settings.height == "undefined"   ? "auto" : settings.height,  
        left:   typeof settings.left == "undefined"       ? "auto" : settings.left,  
        top:    typeof settings.top == "undefined"       ? "auto" : settings.top
    })
    .styles( {
        left:   win.style.left     ? win.offsetLeft+"px" :     win.style.left,
        top:    win.style.top      ? win.offsetTop+"px"  :     win.style.top,
        width:  win.style.width    ? win.scrollWidth+"px" :    win.style.width,
        height: win.style.height   ? win.offsetHeight+"px"  :  win.style.height
    });
    
    bWin.find(".bookyTitleBar")[0].bookyAction = "move";  //shouldn't do this really...need jquery data() like thing
    bWin.find(".bookyTitleBar")[0].onmousedown = booky.windowMouse;   
    bWin.find(".bookyResize")[0].bookyAction = "resize";//dont need todo this- can work it ou on windowMouse
    bWin.find(".bookyResize")[0].onmousedown = booky.windowMouse;

    //minimise   (duplicated code on min and max...)
   
   
   
   
   
   
   
   
   
   //CAN'T USE BWIN TOO MUCYH...SOMETHIGN WRONG
    bWin.find(".bookyMin")[0].onclick = function(e) { 
         
         
        if(bWin.hasClass("bookyMinimized")) {
            bWin.styles(win.restoreStyles);
            win.restoreStyles = undefined;
            bWin.removeClass( "bookyMaximized" )  //redundent code......
                      .removeClass( "bookyMinimized" );    
            return false;
        }
        
       
        if(!bWin.hasClass("bookyMaximized") &&       //redundent code......
           !bWin.hasClass("bookyMinimized") ) {
                win.restoreStyles = {
                    width:  win.style.width,
                    height: win.style.height,
                    left:   win.style.left,
                    top:    win.style.top
                };
        }
        bWin.removeClass(  "bookyMaximized" )    //redundent code...... 
                  .removeClass(  "bookyMinimized" );    //redundent code...... 
        //count number of minimised windows
        var left = (150 * booky(".bookyMinimized").length)+"px"; //border errors here
            
        bWin.styles( {width:"150px", height:"18px", left:left, bottom:0, top:"auto"}) //could add a class on instead?      
                  .addClass("bookyMinimized" );             
    };
    
    
    
    
    
    
 
 
 
 
 
 
    //maximise      
    bWin.find(".bookyMax")[0].onclick = 
    bWin.find(".bookyTitleBar")[0].ondblclick = function() {
        win.onmousedown({currentTarget:win});
        if(bWin.hasClass("bookyMaximized")) {
            
            bWin.styles( win.restoreStyles);
            win.restoreStyles = undefined;
            bWin.removeClass(  "bookyMaximized" );       
            bWin.removeClass( "bookyMinimized" );  
            return false;
        }
        
        if(!bWin.hasClass("bookyMaximized") &&       //redundent code......
           !bWin.hasClass("bookyMinimized") ) {
                win.restoreStyles = {
                    width:  win.style.width,    
                    height: win.style.height,
                    left:   win.style.left,
                    top:    win.style.top
                };
        }    
        
        bWin.removeClass("bookyMaximized" );    //redundent code...... 
        bWin.removeClass("bookyMinimized" );    //redundent code...... 
        bWin.styles({width:"100%", height:"100%", left:0, top:0}); //could add a class on instead?  
        bWin.addClass( "bookyMaximized" );             
    };        
 
    //close button    
    bWin.find(".bookyClose")[0].onmousedown = function(e) {
        this.style.backgroundPosition = "-34px -14px"; /*fix up with css.........*/
    };     
    bWin.find(".bookyClose")[0].onmouseup = function(e) {
        this.style.backgroundPosition = "-34px 0";
    };
      
    bWin.find(".bookyClose")[0].onclick = function(e) {
    
    
        this.style.backgroundPosition = "-34px 0";
        if(!e) e = window.event;
        e.preventDefault();
        
        if(typeof settings.close == "function") settings.close(); 
        booky(this.parentNode.parentNode).rem();
        
    };   
    //stuff is inconsistent
    
    
    
    
    
    return this;
    
    
    
    
    
    
    
    
    
    
    
    
    //test menu
    var menu = {
        settings: {},
        children: [{name: "&File", children: [
                              {name: "New",  action: function() {alert("u clicked new");}},
                              {name: "Open", action: function() {alert("u clicked open!!");}},
                              {name: "Exit", action: function() {alert("u clicked exxxxxit!!");}}
                            ]
                },
                {name: "&Help", children: [
                              {name: "About",  action: function() {alert("u clicked about");}}
                            ]
                }
                ]
    
    }
        
}




/*general dropdown menu*/



//fix index on click, just carries on for ever atm...
booky.zIndex = 10;
booky.bringToTop = function(e) {
    this.style.zIndex = ++booky.zIndex;
    
}





/*general dropdown menu*/
booky.menuWindow = function(m) {    
    var current = m.top,
        frag = booky.getFrag("<ul class=bookyMenu></ul>");
    while(current.children.length > 0) {        
        if(current.children.length > 0)
            current = current.children;
    }
    
    function addMenu() {
        
    }
}









//right click


//menubar menu













/*drag drop*/

//SXTOP IT FROM GOING OIF SCREEEEEEEEEEEN
//need an ultra simple way of simplyfing evenets!!!!!!!!!!!!!!!
booky.windowMouse = function(event) {  
    if(!event) var event = window.event;

    if((this.className != "bookyTitleBar" && this.className != "bookyResize")  ||
    typeof this.parentNode.restoreStyles !== "undefined")
        return false;

        
    var action = this.bookyAction,
        dragged = this.parentNode,
        x = parseInt(dragged.style.left),
        y = parseInt(dragged.style.top),
        width = parseInt(dragged.style.width),
        height = parseInt(dragged.style.height),
        dX = event.clientX - x,
        dY = event.clientY - y;
        
    if(document.addEventListener) {
        document.addEventListener("mousemove", moveHandler, true);
        document.addEventListener("mouseup", upHandler, true);    
    }  else if(document.attachEvent) {  
        dragged.attachEvent("onmousemove", moveHandler);
        dragged.attachEvent("onmouseup", upHandler);   
        dragged.setCapture();           
    }    
   
    if(event.stopPropagation)	event.stopPropagation(); 
    else event.cancelBubble = true;    
        
    if(event.preventDefault)	event.preventDefault();
    else event.returnValue = false;

    function moveHandler(e) {
        if(!e)e=window.event;     
           
        if(action == "move") {
            dragged.style.left = (e.clientX - dX) + "px";
            dragged.style.top = (e.clientY - dY) + "px";
        }
        else if(action == "resize") { 
            dragged.style.width = width + (e.clientX - dX - dragged.offsetLeft) + "px";
            dragged.style.height = height + (e.clientY - dY - dragged.offsetTop) + "px";
        }        
        if(e.stopPropagation)  e.stopPropagation(); 
        else e.cancelBubble = true;        
    }

    function upHandler(e) {
        if(!e)e=window.event;
        if(document.removeEventListener) {
            document.removeEventListener("mouseup", upHandler, true);
            document.removeEventListener("mousemove", moveHandler, true);
        } else if(document.detachEvent) {
            dragged.detachEvent("onmouseup", upHandler);
            dragged.detachEvent("onmousemove", moveHandler);
            dragged.releaseCapture();
        }        
        if(e.stopPropagation)  e.stopPropagation(); 
        else e.cancelBubble = true;    
        
    }
}
booky.confirm = function(type, settings) {    
    switch(type) {
        case "ok":
           var test =  booky(settings.message+"<input onclick='booky(this.parentNode).rem();' type=button value=OK>").window({title: settings.title, cssClass: "bookyConfirm",width:"150px"});    
            break;
         
    }
    
    return this;
}




})();